package com.play.ballen.home.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.hjq.toast.ToastUtils;
import com.play.ballen.R;
import com.play.ballen.home.entity.Version;
import com.play.ballen.utils.ApkDownManger;
import com.play.ballen.utils.MyActivitys;
import com.play.ballen.utils.URLImageParser;
import com.xuexiang.xupdate._XUpdate;

import java.io.File;

/**
 * 更新提示对话框
 */
public class DiaLogActivity extends Activity {
    private View rootView;
    private TextView content,tv_version;
    private TextView btn_exit, btn_update;
    private LinearLayout ll1,ll2;
    private ProgressBar progressBar;

//    private VersionManager.AppVersion appVersion;
    private ApkDownManger apkDownManger;

    private Version version;
    public static void start(Context context, Version version) {
        Intent starter = new Intent(context, DiaLogActivity.class);
        starter.putExtra("version",version);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyActivitys.addActivity(this);
        setContentView(R.layout.actvitiy_up_dialog);
        initView();
        initListener();
    }

    private void initView(){
        version = (Version) getIntent().getSerializableExtra("version");
//        rootView=findViewById(R.id.root_view);
        content = findViewById(R.id.update_content);
        tv_version= findViewById(R.id.tv_version);
        btn_exit =findViewById(R.id.btn_exit);
        btn_update =findViewById(R.id.btn_update);
        ll1=findViewById(R.id.ll_1);
        ll2=findViewById(R.id.ll_2);
        progressBar=findViewById(R.id.progress);
        ll1.setVisibility(View.VISIBLE);
        ll2.setVisibility(View.GONE);
        btn_update.setText(R.string.update_now);
        Spanned result = Html.fromHtml(version.getContent(), new URLImageParser(content), null);
        content.setText(result);
        tv_version.setText(getResources().getString(R.string.version_code)+" "+version.getVersion());
        btn_exit.setVisibility(View.GONE);
        apkDownManger=new ApkDownManger(this,version.getDownload(),this.getExternalFilesDir("down").getPath());
        int state=apkDownManger.getState();
        if(state==2){
            btn_update.setText(R.string.anzhuang_now);
        }else if(state==1){
            ll1.setVisibility(View.INVISIBLE);
            ll2.setVisibility(View.VISIBLE);
        }else{
            btn_update.setText(R.string.update_now);
        }
//        if (appVersion!=null&&appVersion.update) {
//            content.setText(appVersion.hint);
//
//            btn_exit.setVisibility(View.GONE);
////            if (appVersion.forced) {
////                btn_exit.setVisibility(View.GONE);
////            } else {
////                btn_exit.setVisibility(View.VISIBLE);
////            }
//            apkDownManger=new ApkDownManger(this,version.getDownload(),this.getExternalFilesDir("down").getPath());
//            int state=apkDownManger.getState();
//            if(state==2){
//                btn_update.setText("立即安装");
//            }else if(state==1){
//                ll1.setVisibility(View.INVISIBLE);
//                ll2.setVisibility(View.VISIBLE);
//            }else{
//                btn_update.setText("立即更新");
//            }
//        } else {
//            finish();
//        }
    }

    @Override
    public void finish() {
//        rootView.setVisibility(View.GONE);
        super.finish();
    }

    private void initListener(){
//        findViewById(R.id.btn_exit).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if(appVersion.forced){
//                    finish();
//                    MyActivitys.finishAll();
//                    VersionManager.getInstance().reset();
//                    Intent startMain = new Intent(Intent.ACTION_MAIN);
//                    startMain.addCategory(Intent.CATEGORY_HOME);
//                    startActivity(startMain);
//                }else{
//                    VersionManager.getInstance().setShowTime(System.currentTimeMillis());
//                    finish();
//                }
//            }
//        });
        findViewById(R.id.btn_update).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT <Build.VERSION_CODES.LOLLIPOP){//备用防止升级失败
                    Uri uri = Uri.parse(version.getDownload());
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                    return;
                }
                try {
                    int state=apkDownManger.getState();
                    if(state==2){//安装
                        ll1.setVisibility(View.VISIBLE);
                        ll2.setVisibility(View.GONE);
                        File file=new File(apkDownManger.getSavePath());
                        _XUpdate.startInstallApk(DiaLogActivity.this, file); //填写文件所在的路径
                    }else if(state==1){

                    }else{
                        apkDownManger.startDown(new ApkDownManger.IDownBack() {
                            @Override
                            public void onBack(int state,int p) {
                                if(state==2){
                                    btn_update.setText("立即安装");
                                    ll1.setVisibility(View.VISIBLE);
                                    ll2.setVisibility(View.GONE);
                                    File file=new File(apkDownManger.getSavePath());
                                    _XUpdate.startInstallApk(DiaLogActivity.this, file); //填写文件所在的路径
                                }else if(state==1){
                                    ll1.setVisibility(View.INVISIBLE);
                                    ll2.setVisibility(View.VISIBLE);
                                    if (p>0){
                                        progressBar.setProgress(p);
                                    }else{
                                        progressBar.setProgress(0);
                                    }
                                }else if(state==4){
                                    ll1.setVisibility(View.VISIBLE);
                                    ll2.setVisibility(View.GONE);
                                }else if(state==3){
                                    ll1.setVisibility(View.VISIBLE);
                                    ll2.setVisibility(View.GONE);
                                    ToastUtils.show("安装包下载失败，请重试");
                                }else{
                                    ll1.setVisibility(View.VISIBLE);
                                    ll2.setVisibility(View.GONE);
                                }
                            }
                        });
                    }
                }catch (Exception e){
                    Uri uri = Uri.parse(version.getDownload());
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            finish();
            MyActivitys.finishAll();

//            if (appVersion.forced) {
//                finish();
//                MyActivitys.finishAll();
//            } else {
//                finish();
//            }
            return true;
        }else{
            return super.onKeyDown(keyCode, event);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyActivitys.removeActivity(this);
    }
}
